package com.gxh.controller.admin;


import com.gxh.dto.CategoryDTO;
import com.gxh.dto.CategoryPageQueryDTO;
import com.gxh.entity.Category;
import com.gxh.result.PageResult;
import com.gxh.result.Result;
import com.gxh.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/admin/category")
@Api(tags = "分类相关接口")
@Slf4j
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation(value = "分类分页查询")
    public Result<PageResult> page(CategoryPageQueryDTO categoryPageQueryDTO){
        log.info("分页查询{}",categoryPageQueryDTO);
        PageResult pageResult = categoryService.pageQuery(categoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 新增分类
     * @param categoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation(value = "新增分类")
    @CacheEvict(value = "categoryCache",key = "'list'")
    public Result save(@RequestBody CategoryDTO categoryDTO){
        log.info("新增分类{}",categoryDTO);
        categoryService.save(categoryDTO);
        return Result.success();
    }


    /**
     * 修改分类
     * @param categoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改分类")
    @CacheEvict(value = "categoryCache",key = "'list'")
    public Result update(@RequestBody CategoryDTO categoryDTO){
        log.info("修改分类{}",categoryDTO);
        categoryService.update(categoryDTO);
        return Result.success();
    }

    @GetMapping ("/{id}")
    @ApiOperation(value = "根据id查询分类")
    public Result getById(@PathVariable Long id){
        log.info("根据id查询分类{}", id);
        Category category = categoryService.getById(id);
        return Result.success(category);
    }

    /**
     * 批量删除分类
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除分类")
    @CacheEvict(value = "categoryCache",key = "'list'")
    public Result delete(@RequestBody List<Long> ids){
        log.info("批量删除分类{}", ids);
        categoryService.deleteBatch(ids);
        return Result.success();
    }

    @GetMapping("/list")
    @ApiOperation("查询所有分类")
    public Result<List<Category>> list(){
        log.info("查询所有分类");
        List<Category> list = categoryService.list();
        return Result.success(list);
    }

}
